<?php

namespace app\web\controller;
use app\common\controller\StoreBaseController;
use app\admin\model\Member as MemberModel;
use think\Db;
use app\common\model\Member;
/**
 *会员管理
 */
class MemberController extends StoreBaseController
{
 	protected $model;
	
    protected function _initialize() {
        parent::_initialize();
		$this->model=new MemberModel();
    }
    /**
     * 我的会员下级
     * @author [longx] 
     */
    public function index( $page = 1){
    	$uid = session('storeInfo')['u_id'];
		if(request()->isPost()){
			$param = input('post.');
			//会员名称
			if($param['real_name']){
				$where['real_name'] = ['like', '%'.$param['real_name'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$where['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}
		}
		$where['pid']=$uid;
		$list=$this->model->where($where)->paginate(20);
     	return $this->fetch('index', ['list' => $list]);
    }
	    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$uid = session('storeInfo')['u_id'];
			$param['pid']=$uid;
			$param['add_time'] = now_time();
			if ($this->model->allowField(true)->save($param) !== false) {
                $this->success('添加成功', url('member/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
	/**
     * [edit 编辑]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                $this->success('修改成功', url('member/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $ad=$this->model->find(input("id"));
		$this->assign("ad",$ad);
        return $this->fetch();
    }
	//充值记录
    public function memberInfo(){
    	$where['tid']=session('storeInfo')['id'];
		$where['uid']=input("id");
		$where['credittype']='credit1';
    	$list=db('_mall_credits')->where($where)->select();
		$this->assign("id",input("id"));
		return $this->fetch('memberInfo', ['list' => $list]);
    }
       /**
     * 会员充值
     * @param int    $page
     * @return mixed
     */
    public function chongzhi_member() {
    	$member_model= new Member();
		$member=$this->model->find(input("id"));
    	if(input("type")==2){//余额
			$message='余额充值';
			$member_model->mc_credit_inc($member, "credit2", input("value"), $operator = 0, $message,6,0,0);
    	}else{//积分充值同步到线上
    		$agent=db("mall_agent")->where("id",session('storeInfo')['id'])->find();
			if($agent['score']<input("value")){
				json(["code"=>400,'msg'=>'店铺积分不足']);
			}
			$message="积分充值";
			$member_model->mc_credit_inc($member, "credit1", input("value"), $operator = 0, $message,6,$agent['id'],0);
			$member_model->insscoch($agent['id'],'会员积分充值扣除积分',input("value"),4,$tid=0);
    	}
        return json(["code"=>200,'msg'=>'充值成功']);
       
    }
//导出csv文件

  public function dc($data){
  	$uid = session('storeInfo')['u_id'];
		$field = 'm.*,g.uid,g.level,g.son_id';
		$where['g.level']=$data;
		$where['g.uid']=$uid;
		$result=Db::name("mall_level")->alias('g')->join('__MEMBER__ m','m.id=g.son_id')->field($field)->where($where)->select();
        $str = "姓名,电话,时间,\n"; 
        $str = iconv('utf-8','gbk',$str);

        foreach($result as $k=>$row){
                $name = iconv('utf-8','gbk',$row['real_name']); //中文转码 
                $mobile = iconv('utf-8','gbk',$row['mobile']);                 
                
                $time = iconv('utf-8','gbk',$row['add_time']);                 
                $str .= $name.",".$mobile.",".$time.","."\n"; //用引文逗号分开 
            } 
      
		if($data == '1'){
	$filename ='一级会员表.csv';   //设置文件名  
			}
		if($data == '2'){
	$filename ='二级会员表.csv';   //设置文件名  
			}
		if($data == '3'){
	$filename ='三级会员表.csv';   //设置文件名  
			}
		
        	
  
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;

     }


}